<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
if(isset($_SESSION['lang']))
    require_once "./language_files/rebuttal_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/rebuttal_en.php";
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <?php include 'jquery.php';?>
        <script type="text/javascript" src="functions.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                check_top();
                $(document).on("click", ".addR", function(){
                    paperID = $(this).attr("paperID");
                    commentID = $(this).attr("commentID");
                    bt = $(this);
                    input =  $("textarea", $(this).parent());
                    text = $("textarea", $(this).parent()).val();
                    $.ajax ({
                        data:{paperID:paperID, commentID:commentID, text:text},
                        type: 'POST',
                        url:  'add_rebuttal.php',
                        success: function(response) {
                            if(response == 1) {
                                alert("Rebuttal sent to Organizer successfully");
                                window.location.href=window.location.href;
                            } else if(response == 0) {
                                alert("all fields are required!");
                            } else {
                                alert(response);
                            }
                        }
                    });

                });
            });
        </script>
        <title>Rebuttal</title>
    </head>
    <body>
        <?php
        require_once 'functions.php';
        if(isset($_GET['paper_id']))
        {
            //echo 'in if';
            $member_id=$_SESSION['member_id'];
            //$_SESSION['member_id'];// getting the member id
            $paper_id=$_GET['paper_id'];
            $conference_id=$_GET['conference_id'];
            $confName=get_conference_name($conference_id);
            echo"</br><span class=breadcrumbs><a href='subscriberConferencePage.php?conference_id={$conference_id}'>" . $confName . "</a>" . " > " . $lang['Rebuttal'] . " </span> </br></br>";
            $date=date('Y-m-d');
            $today=date("Y-m-d H:i:s",strtotime($date));
            //getting the deadline
            $comments=mysql_query("SELECT c.comment_id, c.text, c.version_id, c.commentor_id FROM comment c
                INNER JOIN version v ON (v.version_id = c.version_id)
                INNER JOIN paper p ON (p.paper_id = v.paper_id)
                INNER JOIN conference cc  ON (cc.conference_id = p.conference_id)
                WHERE cc.conference_id = $conference_id
                AND p.paper_id = $paper_id") or die(mysql_error());
            echo "<table>";

            $deadline=mysql_query("SELECT `rev_endDate`
            From`conference`c
            Where c.`conference_id`='$conference_id'
            ") or die(mysql_error());
            $re_date=mysql_query("SELECT rebuttal_date FROM conference
               WHERE conference_id = $conference_id") or die(mysql_error());
            if(mysql_num_rows($re_date) > 0)
            {
                while($re=mysql_fetch_assoc($re_date))
                {
                    $de=$re['rebuttal_date'];
                }
            }
            if($de != null)
            {
                echo "<td colspan='10'><label class='float_left'><strong>" . $lang['message_5'] . "<b>$de</b></strong></label></td>";


                $d=strtotime($de);
                $re_dead=date("Y-m-d",$d);
                $today=date("Y-m-d");
                if($re_dead > $today)
                {
                    echo ' <tr>
        <td style="background-color:#003366; color:white; text-align:center;"><b>' . $lang['reviewer'] . '</b></td>
        <td style="background-color:#003366; color:white; text-align:center;"><b>' . $lang['Rebuttal'] . '</b></td>
            </tr>';
                    if(mysql_num_rows($comments) == 0)
                    {
                        echo "<tr><td style='text-align:center' colspan='2'>There are no Reviews added to this paper yet</td></tr>";
                    }
                    while($row=mysql_fetch_assoc($comments))
                    {

                        echo "<tr><td>{$row['text']}</td>";
                        $pre_value=mysql_query("SELECT rebuttal_text FROM a_rebuttal_paper
                    WHERE comment_id = {$row['comment_id']}") or die(mysql_error());
                        $pre="";
                        if(mysql_num_rows($pre_value) > 0)
                        {
                            while($r=mysql_fetch_assoc($pre_value))
                            {
                                $pre=formatText_safe($r['rebuttal_text']);
                                echo "<td>$pre</td></tr>";
                            }
                        }
                        else
                        {
                            ?>
                            <td><textarea class="reText" rows='5' name='reText' id='reText' style='width:98%;' type='text'><?php echo $pre?></textarea>
                                <button commentID="<?php echo $row['comment_id']?>" paperID="<?php echo $paper_id?>" class="addR" type="button" name="addR" id="addR">send rebuttal</button></td></tr> <?
                }
            }
        }
        else
        {
            echo $lang['text'];
        }
    }
    else
    {
        echo ' <tr>
        <td style="background-color:#003366; color:white; text-align:center;"><b>' . $lang['reviewer'] . '</b></td>
        <td style="background-color:#003366; color:white; text-align:center;"><b>' . $lang['Rebuttal'] . '</b></td>
            </tr>';
        if(mysql_num_rows($comments) == 0)
        {
            echo "<tr><td style='text-align:center' colspan='2'>There are no Reviews added to this paper yet</td></tr>";
        }
        while($row=mysql_fetch_assoc($comments))
        {

            echo "<tr><td>{$row['text']}</td>";
            $pre_value=mysql_query("SELECT rebuttal_text FROM a_rebuttal_paper
                    WHERE comment_id = {$row['comment_id']} AND paper_id = {$row['version_id']}") or die(mysql_error());
            $pre="";
            if(mysql_num_rows($pre_value) > 0)
            {
                while($r=mysql_fetch_assoc($pre_value))
                {
                    $pre=formatText_safe($r['rebuttal_text']);
                    echo "<td><div>$pre</div></td></tr>";
                }
            }
            else
            {
                ?>
                        <td><textarea class="reText" rows='5' name='reText' id='reText' style='width:98%;' type='text'><?php echo $pre?></textarea>
                            <button commentID="<?php echo $row['comment_id']?>" paperID="<?php echo $paper_id?>" class="addR" type="button" name="addR" id="addR">send rebuttal</button></td></tr> <?
            }
        }
    }
    echo "</table>";
}
        ?>
    </body>
</html>
